<?php

class UploadFile
{
    public $field_name = "";
    public $file_ext = "txt";
    public $save_path = ".";
    public $save_filename = "";
    public $has_file = False;
    public $is_save = False;
    
    public function UploadFile ($field_name) {
        $this->field_name = $field_name;
        if ($this->hasUploadFile()) {
            $this->has_file = True;
            $filestr = $_FILES[$this->field_name]["name"];
            $this->file_ext = $this->getFileExtend($filestr);
            $this->randFileName();
        }
    }
    
    public function hasUploadFile () {
        if (!array_key_exists($this->field_name, $_FILES)) {
            return false;
        }
        return $_FILES[$this->field_name]["error"] == 0;
    }
    
    public function randFileName () {
        $result = '';
        $result .= microtime();
        $result .= rand(10000, 99999);
        $result = preg_replace('/[\. ]/', '', $result);
        $this->save_filename = $result . "." . $this->file_ext;

    }
    
    public function getFileExtend ($filestr) {
        $extend = explode(".", $filestr);
        $va = count($extend) - 1;
        return $extend[$va];
    }
    
    public function save () {
        $src_filename = $_FILES[$this->field_name]["tmp_name"];
        $dest_filename = $this->save_path . "/" . $this->save_filename;
        return move_uploaded_file($src_filename, $dest_filename);
    }
    

    public function inType ($extlist) {
        return in_array($this->file_ext, $extlist);
    }
    
}

